package com.cmwa.scbp.sys.userInfo.service.impl;

import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.cmwa.scbp.base.mvc.entity.Employee;
import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.sys.organization.entity.OrganizationVo;
import com.cmwa.scbp.sys.organization.service.OrganizationService;
import com.cmwa.scbp.sys.userInfo.dao.UserInfoDao;
import com.cmwa.scbp.sys.userInfo.entity.OrmemberVo;
import com.cmwa.scbp.sys.userInfo.entity.UserInfoVo;
import com.cmwa.scbp.sys.userInfo.service.UserInfoService;
import com.cmwa.scbp.base.constants.Constant;
import com.cmwa.scbp.utils.PwdUtil;
import com.cmwa.scbp.utils.SessionUtils;

@Service
public class UserInfoServiceImpl implements UserInfoService {
	
	@Resource
	private UserInfoDao userInfoDao;

	@Resource
	private OrganizationService organizationService;

	@Override
	public List<UserInfoVo> searchUserInfoList(UserInfoVo userInfoVo) {
		
		return userInfoDao.searchUserInfoList(userInfoVo);
	}

	@Override
	public List<UserInfoVo> searchDepartmentList(UserInfoVo userInfoVo) {
		return userInfoDao.searchDepartmentList(userInfoVo);
	}

	@Override
	public String queryOrgIdByEmpId(String empId) {
		UserInfoVo userInfoVo=new UserInfoVo();
		userInfoVo.setEmpID(empId);
		List<UserInfoVo> userInfoVos=	userInfoDao.searchUserInfoList(userInfoVo);
		if (userInfoVos!=null && userInfoVos.size()!=0) {
			return userInfoVos.get(0).getSecondOrgid();
		}
		return null;
	}

	@Override
	public List<UserInfoVo> queryAllDepartmentList() {
		return userInfoDao.queryAllDepartmentList();
	}

	@Override
	public List<UserInfoVo> queryEmpListByStationId(String orgId, String stationId) {
		return userInfoDao.queryEmpListByStationId(orgId,stationId);
	}

	@Override
	public List<UserInfoVo> searchUserInfo(UserInfoVo userInfoVo) {
		return userInfoDao.searchUserInfo(userInfoVo);
	}

	@Override
	public UserInfoVo getUserInfoById(String empId) {
		if (StringUtils.isNotBlank(empId)) {
			UserInfoVo userInfoVo = new UserInfoVo();
			userInfoVo.setEmpID(empId);
			List<UserInfoVo> userList = searchUserInfo(userInfoVo);
			if (CollectionUtils.isNotEmpty(userList)) {
				return userList.get(0);
			}
		}
		return null;
	}

	@Override
	public List<UserInfoVo> searchUserLeaderByOrgId(String empId) {
		return userInfoDao.searchUserLeaderByOrgId(empId);
	}

	@Override
	public List<UserInfoVo> queryAllMatchDepartmentList(String orgName) {
		List<UserInfoVo> departmentList = userInfoDao.queryAllMatchDepartmentList();
		List<UserInfoVo> vList = new Vector<UserInfoVo>();
		if ((departmentList == null || departmentList.size() == 0)) {
			return vList;
		} else {
			for (int i = 0; i < departmentList.size(); i++) {
				UserInfoVo vo = (UserInfoVo) departmentList.get(i);
				if (vo == null) {
					continue;
				}
				String name = "";

				if (vo.getReOrgName() != null) {
					name += vo.getReOrgName();
				}
				if (vo.getOrgName() != null) {
					name += vo.getOrgName();
				}
				if (vo.getSecondOrgname() != null) {
					name += vo.getSecondOrgname();
				}
				if (vo.getTitleName() != null) {
					name += vo.getTitleName();
				}
				if (name.indexOf(orgName) > -1) {
					vList.add(vo);
				}
			}
			return vList;
		}
	}

	@Override
	public Page<UserInfoVo> userListPage(SearchParam sp) {
		String nodeId = sp.getSp().get("nodeId") == null ? "" : sp.getSp().get("nodeId").toString();
		
		List<UserInfoVo> items = null;
		
		if(StringUtils.isNotBlank(nodeId)) {
			if(nodeId.startsWith(Constant.USERTREE_STATION_PREFIX)) {  // 岗位
				String[] orgIds = nodeId.substring(Constant.USERTREE_STATION_PREFIX.length()).split(Constant.USERTREE_SPILT);
				String orgId = orgIds[0];
				String stationId = orgIds[1];
				
				sp.getSp().put("searchType", "dept");
				sp.getSp().put("orgId", orgId);
				sp.getSp().put("stationId", stationId);
				
				
			} else if(nodeId.startsWith(Constant.USERTREE_EMP_PREFIX)) {  // 人员
				
				String[] orgIds = nodeId.substring(Constant.USERTREE_EMP_PREFIX.length()).split(Constant.USERTREE_SPILT);
				String empId = orgIds[0];
				sp.getSp().put("searchType", "emp");
				sp.getSp().put("empId", empId);
				
			} else if(nodeId.startsWith(Constant.USERTREE_FIRM_PREFIX)) {
				
				String[] orgIds = nodeId.substring(Constant.USERTREE_FIRM_PREFIX.length()).split(Constant.USERTREE_SPILT);
				String orgId = orgIds[0];
				
				sp.getSp().put("searchType", "firm");
				sp.getSp().put("reOrgId", orgId);
			} else {  // 部门
				sp.getSp().put("searchType", "firm");
				sp.getSp().put("reOrgId", nodeId);
			}
		} else {
//			sp.getSp().put("searchType", "firm");
//			sp.getSp().put("reOrgId", "217CMWA");
		}
		items = userInfoDao.userListPage(sp);
		
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}

	@Override
	public void insertOutUser(UserInfoVo user) {
		Employee employee = SessionUtils.getEmployee();
		String empId = employee.getID();
		
		// 1、新增人员
		user.setCreateId(empId);
		
		PwdUtil encoderMd5 = new PwdUtil(user.getLogin(), PwdUtil.STRATEGY_MD5);
		String encodePwd = encoderMd5.encode(user.getPassword());
		user.setPassword(encodePwd);
		
		userInfoDao.insertOutUser(user);

		String secondOrgname = user.getSecondOrgname();
		if(StringUtils.isNotBlank(secondOrgname)) {
			if(secondOrgname.indexOf("NewTag_") != -1) {
				secondOrgname = secondOrgname.replace("NewTag_", "");
			} else if(secondOrgname.indexOf("_") != -1) {
				secondOrgname = secondOrgname.split("_")[1];
			}
		}
		
		String titleName = user.getTitleName();
		if(StringUtils.isNotBlank(titleName)) {
			if(titleName.indexOf("NewTag_") != -1) {
				titleName = titleName.replace("NewTag_", "");
			} else if(titleName.indexOf("_") != -1) {
				titleName = titleName.split("_")[1];
			}
		}
		
		if(!user.getExistSecondOrg()) {
			// 2、新增部门
			OrganizationVo org = new OrganizationVo();
			org.setOrgId(user.getSecondOrgid());
			org.setOrgName(secondOrgname);
			org.setReOrgId(user.getOrgId());
			org.setReOrgName(user.getOrgName());
			org.setOrgType("OUT");
			organizationService.insertOutOrg(org);
		}
		
		// 3、新增岗位
		OrmemberVo ormemberVo = new OrmemberVo();
		ormemberVo.setOrgId(user.getSecondOrgid());
		ormemberVo.setOrgName(secondOrgname);
		ormemberVo.setTitleId(user.getTitleId());
		ormemberVo.setEmpId(user.getEmpID());
		ormemberVo.setEmName(user.getEmpName());
		ormemberVo.setTitleName(titleName);
		ormemberVo.setCreateId(empId);
		
		userInfoDao.insertOrMember(ormemberVo);
	}

	@Override
	public UserInfoVo getUserInfoLogin(String emNamePinYin) {
		return userInfoDao.getUserInfoLogin(emNamePinYin);
	}

	@Override
	public boolean checkRepeatLogin(String login) {
		UserInfoVo user = userInfoDao.getUserInfoLogin(login);
		if(user == null) {
			return false;
		}
		return true;
	}

	@Override
	public void modifyPassword(String empId, String login, String password) {
		PwdUtil encoderMd5 = new PwdUtil(login, PwdUtil.STRATEGY_MD5);
		String encodePwd = encoderMd5.encode(password);
		userInfoDao.modifyPassword(empId, encodePwd);
	}

	@Override
	public void changeEmpStat(String empId, String stat) {
		
		userInfoDao.changeEmpStat(empId, stat);
	}
}